<!doctype html>
<?php session_start();?>
<html>
	<head>
		<meta charset="utf-8">
		<title>商城后台</title>
		
		<script type="text/javascript">
			function doDel(id){
				if(confirm("确定删除")){
					//跳转
					window.location.href="action.php?a=del&id="+id;
				}
			}
		</script>
		<link href="../include/css/css.css" type="text/css" rel="stylesheet" />
		<link href="../include/css/main.css" type="text/css" rel="stylesheet" />
		<link rel="shortcut icon" href="../include/images/main/favicon.ico" />
		<style>
			body{overflow-x:hidden; background:#f2f0f5; padding:15px 0px 10px 5px;}
			#searchmain{ font-size:12px;}
			#search{ font-size:12px; background:#548fc9; margin:10px 10px 0 0; display:inline; width:100%; color:#FFF; float:left}
			#search form span{height:40px; line-height:40px; padding:0 0px 0 10px; float:left;}
			#search form input.text-word{height:24px; line-height:24px; width:180px; margin:8px 0 6px 0; padding:0 0px 0 10px; float:left; border:1px solid #FFF;}
			#search form input.text-but{height:24px; line-height:24px; width:55px; background:url(../include/images/main/list_input.jpg) no-repeat left top; border:none; cursor:pointer; font-family:"Microsoft YaHei","Tahoma","Arial",'宋体'; color:#666; float:left; margin:8px 0 0 6px; display:inline;}
			#search a.add{ background:url(../include/images/main/add.jpg) no-repeat -3px 7px #548fc9; padding:0 10px 0 26px; height:40px; line-height:40px; font-size:14px; font-weight:bold; color:#FFF; float:right}
			#search a:hover.add{ text-decoration:underline; color:#d2e9ff;}
			#main-tab{ border:1px solid #eaeaea; background:#FFF; font-size:12px;}
			#main-tab th{ font-size:12px; background:url(../include/images/main/list_bg.jpg) repeat-x; height:32px; line-height:32px;}
			#main-tab td{ font-size:12px; line-height:40px;}
			#main-tab td a{ font-size:12px; color:#548fc9;}
			#main-tab td a:hover{color:#565656; text-decoration:underline;}
			.bordertop{ border-top:1px solid #ebebeb}
			.borderright{ border-right:1px solid #ebebeb}
			.borderbottom{ border-bottom:1px solid #ebebeb}
			.borderleft{ border-left:1px solid #ebebeb}
			.gray{ color:#dbdbdb;}
			td.fenye{ padding:10px 0 0 0; text-align:right;}
			.bggray{ background:#f9f9f9}
		</style>
	</head>
<body>
<!--main_top-->
<table width="99%" border="0" cellspacing="0" cellpadding="0" id="searchmain">
  <tr>
    <td width="99%" align="left" valign="top">您的位置：会员管理</td>
  </tr>
  <tr>
    <td align="left" valign="top">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="search">
  		<tr>
   		 <td width="90%" align="left" valign="middle">
	         <form action="index.php" method="get">
	         <span>会员查询：</span>
	         <input input type="text" name="name" class="text-word">
				
	        <input type="submit" value="搜索" class="text-but"> 
			<select name="sex" style="margin-top:10px;margin-left:5px;">
				<option value="">-性别-</option>
				<option value="1">男</option>
				<option value="0">女</option>
			</select>
	       </form>
	</table>
    </td>
  </tr>
  <tr>
    <td align="left" valign="top">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="main-tab">
      <tr>
		 <th align="center" valign="middle" class="borderright">编号</th>
		 <th align="center" valign="middle" class="borderright">账号</th>
		 <th align="center" valign="middle" class="borderright">真实姓名</th>
		 <th align="center" valign="middle" class="borderright">性别</th>
		 <th align="center" valign="middle" class="borderright">邮编</th>
		 <th align="center" valign="middle" class="borderright">电话</th>
		 <th align="center" valign="middle" class="borderright">Email</th>
		 <th align="center" valign="middle" class="borderright">状态</th>
		 <th align="center" valign="middle" class="borderright">注册时间</th>
		 <th align="center" valign="middle" class="borderright">修改权限</th>
		 <th align="center" valign="middle">操作</th>
      </tr>
	  <?php
			$wherelist = array();//定义一个搜索条件的空数组
			$urllist = array();		//定义一个搜索条件的url数组 用于做url参数
			//判断获取有没有填搜索条件
			if(!empty($_GET['name'])){
				$wherelist[] = "name like '%{$_GET['name']}%'";
				$urllist[] = "name ={$_GET['name']}";
			}
			
			if($_GET['sex']!=""){
				$wherelist[]="sex='{$_GET['sex']}'";
				$urllist[]="sex={$_GET['sex']}";
			}
			
			//判断数组是否为空,不为空则拼接sql语句
			if(count($wherelist)>0){
				$where = " where ".implode(" and ",$wherelist);
				$url = "&".implode("&",$urllist);
			}
			// echo '$where='.$where;
			// echo '<br>$url'.$url;
			date_default_timezone_set("PRC");//定义时间的时区
			
			$sex = array(0=>"女",1=>"男");		//从数据库中获取数据后在页面进行转换显示
			$state = array(0=>"管理员",1=>"用户",2=>"禁用");	//从数据库中获取数据后在页面进行转换显示
			
			require("../../public/config.php");		//导入配置文件
			
			$link = @mysqli_connect(HOST,USER,PASS,DBNAME);		//连接数据库
			
			if(!$link){
				die("数据库连接失败！原因是：".mysqli_connect_error());		//数据库连接的判断
			}
			mysqli_set_charset($link,"utf8");	//定义字符集
			
			
			
			
			//=======分页======================================
			// 分页公式 (当前页-1)*页大小,页大小
			//定义变量
			$page = isset($_GET['p'])?$_GET['p']:1;
			$pageSize = 5;		//定义每页显示多少条
			$maxRows = 0;		//定义总条数
			$maxPage = 0;		//定义总页数
			
			//判断是不是超级管理员登录   获取总条数
			if($_SESSION['adminuser']['username']!='admin2'){
				if ($where=="") {
					$where = " where state!=0 ".implode(" and ",$wherelist);
				}else{
					$where = $where."and state!=0";
				}
				
			}
			//超级管理员登录则执行全部
			$sql = "select count(*) as num from users ".$where;
			// echo $sql."<br>";
			$result = mysqli_query($link,$sql);
			$row = mysqli_fetch_assoc($result);
			$maxRows = $row['num'];
			// echo $maxRows;
			//计算总页数
			$maxPage = ceil($maxRows/$pageSize);
			
			if($page>$maxPage){
				$page = $maxPage;
			}
			
			if($page<1){
				$page = 1;
			}
			//拼装limit分页语句
			$limit = " limit ".(($page-1)*$pageSize).",".$pageSize;
			
			//=========结束==============================================
			
			//4.定义sql语句 并发送 判断是不是超级管理员登录
			if($_SESSION['adminuser']['username']!='admin2'){
				$sql = "select * from users".$where." and state!=0 ".$limit;
			}else{
				$sql = "select * from users".$where.$limit;
			}

			// $sql = "select * from users".$where.$limit;
			// echo $sql;
			$result = mysqli_query($link,$sql);
			
		
			// $sql = "select * from users {$where}";		//定义sql语句并发送
			// $result = mysqli_query($link,$sql);	
			if($result){						//遍历并解析结果集
				while($row = mysqli_fetch_assoc($result)){
					echo '<tr onMouseOut="this.style.backgroundColor=\'#ffffff\'" onMouseOver="this.style.backgroundColor=\'#edf5ff\'">';
					echo '<td align="center" valign="middle" class="borderright borderbottom">'.$row['id'].'</td>';
					echo '<td align="center" valign="middle" class="borderright borderbottom">'.$row['username'].'</td>';
					echo '<td align="center" valign="middle" class="borderright borderbottom">'.$row['name'].'</td>';
					echo '<td align="center" valign="middle" class="borderright borderbottom">'.$sex[$row['sex']].'</td>';
					echo '<td align="center" valign="middle" class="borderright borderbottom">'.$row['code'].'</td>';
					echo '<td align="center" valign="middle" class="borderright borderbottom">'.$row['phone'].'</td>';
					echo '<td align="center" valign="middle" class="borderright borderbottom">'.$row['email'].'</td>';
					echo '<td align="center" valign="middle" class="borderright borderbottom">'.$state[$row['state']].'</td>';
					echo '<td align="center" valign="middle" class="borderright borderbottom">'.date("Y-m-d H:i:s",$row['addtime']).'</td>';
					echo '<td align="center" valign="middle" class="borderright borderbottom"><a href="action.php?a=up&id='.$row['id'].'">管理员</a>
							<a href="action.php?a=start&id='.$row['id'].'">用户</a>
							<a href="action.php?a=stop&id='.$row['id'].'">禁用</a></td>';
					echo '<td align="center" valign="middle" class="borderbottom">
							<a href="edit.php?id='.$row['id'].'" target="mainFrame" onFocus="this.blur()" class="add">编辑</a>
							<span class="gray">&nbsp;|&nbsp;</span>
							<a href="javascript:doDel('.$row['id'].')" target="mainFrame" onFocus="this.blur()" class="add">删除</a>
							</td>';
					echo '</tr>';
				}
				mysqli_free_result($result);   //释放结果集
				mysqli_close($link);		//关闭数据库
			}else{
				die("获取数据失败！原因是：".mysqli_error($link));
			}
	  ?>
		</table></td>
		</tr>
		  <tr>
			<?php
			   echo	'<td align="left" valign="top" class="fenye">共'.$maxRows.'条数据 '.$page.'/'.$maxPage.'页&nbsp;&nbsp;';
			   echo '<a href="index.php?p=1'.$url.'" target="mainFrame" onFocus="this.blur()">首页</a>&nbsp;&nbsp;';
			   echo	'<a href="index.php?p='.($page-1).$url.'" target="mainFrame" onFocus="this.blur()">上一页</a>&nbsp;&nbsp;';
			   echo	'<a href="index.php?p='.($page+1).$url.'" target="mainFrame" onFocus="this.blur()">下一页</a>&nbsp;&nbsp;';
			   echo '<a href="index.php?p='.$maxPage.$url.'" target="mainFrame" onFocus="this.blur()">尾页</a></td>';
			?>
		  </tr>
		</table>
	</body>
</html>